package com.tanhua.server.utils;

import java.time.Duration;
import java.time.LocalDateTime;

/**
 * @Description AUTO
 * @ClassName LocalDateTimeInterval
 * @Author ZhengXinjie
 * @date 2021.03.05 10:17
 */
public class LocalDateTimeInterval {
    public static Long geInterval(LocalDateTime localDateTime){
        int year = localDateTime.getYear();
        int month = localDateTime.getMonth().getValue();
        int dayOfMonth = localDateTime.getDayOfMonth();
        // 指定每天的24点
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                if (dayOfMonth + 1 > 31 && month < 12) {
                    month = month + 1;
                    dayOfMonth = 1;
                } else if (dayOfMonth + 1 > 31) {
                    month = 1;
                    dayOfMonth = 1;
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                if (dayOfMonth + 1 > 30) {
                    month = month + 1;
                    dayOfMonth = 1;
                }
                break;
            case 2:
                if (year % 4 == 0 || year % 100 == 0) {
                    // 闰年
                    if (dayOfMonth + 1 > 29) {
                        month = 3;
                        dayOfMonth = 1;
                    }
                } else {
                    if (dayOfMonth + 1 > 28) {
                        month = 3;
                        dayOfMonth = 1;
                    }
                }
                break;
            default:
                throw new RuntimeException();
        }
        LocalDateTime localDateTime1 = LocalDateTime.of(year, month, dayOfMonth + 1, 0, 0, 0);

        Duration duration = Duration.between(localDateTime, localDateTime1);

        return duration.toMillis();
    }
}
